package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.entity.AActiveTypeEntity;
import com.company.project.entity.AClassEntity;
import com.company.project.service.AActiveTypeService;
import com.company.project.service.AClassService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.AActiveEntity;
import com.company.project.service.AActiveService;

import javax.validation.Valid;


/**
 * ${comments}
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2022-06-06 10:11:28
 */
@Controller
@RequestMapping("/")
public class AActiveController {
    @Autowired
    private AActiveService aActiveService;
    @Autowired
    private AClassService aClassService;
    @Autowired
    private AActiveTypeService aActiveTypeService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/aActive")
    public String aActive() {
        return "aactive/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("aActive/add")
    @RequiresPermissions("aActive:add")
    @ResponseBody
    public DataResult add(@RequestBody @Valid AActiveEntity aActive){
        //查询判断班级id和活动类别id是否存在
        AClassEntity aClass = aClassService.getById(aActive.getClassId());
        if(null==aClass){
            throw new BusinessException("该班级ID不存在");
        }
        AActiveTypeEntity aActiveType = aActiveTypeService.getById(aActive.getTypeId());
        if(null==aActiveType){
            throw new BusinessException("该活动类别ID不存在");
        }
        aActive.setClassName(aClass.getName());
        aActive.setTypeName(aActiveType.getName());
        aActiveService.save(aActive);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("aActive/delete")
    @RequiresPermissions("aActive:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        aActiveService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("aActive/update")
    @RequiresPermissions("aActive:update")
    @ResponseBody
    public DataResult update(@RequestBody @Valid AActiveEntity aActive){
        if(null==aActive.getId()){
            throw new BusinessException("活动ID不存在");
        }
        //查询判断班级id和活动类别id是否存在
        AClassEntity aClass = aClassService.getById(aActive.getClassId());
        if(null==aClass){
            throw new BusinessException("该班级ID不存在");
        }
        AActiveTypeEntity aActiveType = aActiveTypeService.getById(aActive.getTypeId());
        if(null==aActiveType){
            throw new BusinessException("该活动类别ID不存在");
        }
        aActive.setClassName(aClass.getName());
        aActive.setTypeName(aActiveType.getName());
        aActiveService.updateById(aActive);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("aActive/listByPage")
    @RequiresPermissions("aActive:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody AActiveEntity aActive){
        Page page = new Page(aActive.getPage(), aActive.getLimit());
        LambdaQueryWrapper<AActiveEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.like(StringUtils.isNotBlank(aActive.getName()),AActiveEntity::getName, aActive.getName());
        IPage<AActiveEntity> iPage = aActiveService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
